﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using XSTDZ.ERP.Application;
using XSTDZ.ERP.Infrastructure;
using XSTDZ.ERP.Infrastructure.Authentication;
using XSTDZ.ERP.Application.DataContracts;
using XSTDZ.ERP.WebApp.Models;
using XSTDZ.ERP.WebApp.Models.Ado;

namespace XSTDZ.ERP.WebApp.Controllers
{
    /// <summary>
    /// 计件表
    /// </summary>
    public class PieceController : Controller
    {
        //
        // GET: /Piece/
        private readonly IWorkGroupService workGroupServiceImpl = ServiceLocator.Instance.GetService<IWorkGroupService>();
        public ActionResult Index()
        {
            return View();
        }
        #region 查看详细
        [NoAuthorizated("PieceDetail")]
        public ActionResult PieceDetail(Guid id)
        {
            var billModel = workGroupServiceImpl.GetPiece(id);
            return View(billModel);
        }
        #endregion

        #region 保存和提交统一入口
        [NoAuthorizated("AddAndConfirm")]
        [HttpPost]
        public ActionResult AddAndConfirm(PieceDto roductInBillDto)
        {
            bool isConfirm = Convert.ToBoolean(Request["isConfirm"]);

            Guid orgId = Guid.Empty;
            //订单已确认
            if (roductInBillDto.BillState > 0)
            {
                return new EmptyResult();
            }
            //保存(或更新)
            var contentRst = Add(roductInBillDto) as ContentResult;
            string actionResult = contentRst.Content.ToString();
            Guid tempId = Guid.Empty;
            if (Guid.TryParse(actionResult.ToString(), out tempId))
            {
                roductInBillDto.Id = tempId;
            }
            else
            {
                return Content(actionResult.ToString());
            }
            //提交
            if (isConfirm)
            {


                if (roductInBillDto.Id != null && !roductInBillDto.Id.Equals(Guid.Empty))//记录已存在
                {

                    Confirm(roductInBillDto.Id??Guid.Empty);//提交

                }
            }
            return Content("9");
        }
        #endregion
        #region 计件表
        [NoAuthorizated("Add")]
        public ActionResult Add(Guid? id)
        {
            PieceDto wg = new PieceDto();
            wg.SubmitDate = DateTime.Today;
            if (id.HasValue)
            {
                Guid wid = Guid.Empty;
                Guid.TryParse(id.Value.ToString(), out wid);
                 wg = workGroupServiceImpl.GetPiece(wid);
            }
            return View(wg);
        }
        [HttpPost]
        [NoAuthorizated("Add")]
        public ActionResult Add(PieceDto workGroup)
        {
            #region 处理介面元素

             var model = new ProductCheckBillModel();
             var wId = model.GetRequestFormValues(this.HttpContext, "UserName_val");
             if (!Guid.Empty.Equals(wId))
            {
                workGroup.WorkGroupID = wId;
            }
            
         
            #endregion

             if (ModelState.IsValid)
             {
                 var id = this.workGroupServiceImpl.AddPiece(workGroup);
                 return Content(id.ToString());
             }
            return Content("0");
        }
        [NoAuthorizated("Delete")]
        public ActionResult Delete(Guid id)
        {
            workGroupServiceImpl.DeleteWorkGroup(id);
            return Content("9");
        }
        #endregion
        #region 获得计件表列表
        [NoAuthorizated("GetJsonOfPiece")]
        public ActionResult GetJsonOfPiece()
        {
            #region EF的查询，效率低下，故暂时放弃不用
            //int recordCount=0;
            //var workGroupService = workGroupServiceImpl.GetPiece(out recordCount, 1, 100);
            //if (workGroupService != null && workGroupService.Count() != 0)
            //{
            //    var jsonResult = workGroupService.Select(u => { u.SettleState = u.IsSettle == 0 ? "未结算" : "结算"; return u; });
            //    var target = Newtonsoft.Json.JsonConvert.SerializeObject(jsonResult);
            //    target = "{\"Rows\":" + target + ",\"Total\":\"" + recordCount + "\"}";
            //    return Content(target);
            //}
            #endregion
            return Content(new BLLQuery().GetPieceLists(this.HttpContext));
        }
        #endregion

        [NoAuthorizated("Confirm")]
        public ActionResult Confirm(Guid billId)
        {
            try
            {
                if (!billId.Equals(Guid.Empty))
                {
                    this.workGroupServiceImpl.Confirm(billId);
                    return Content("9");
                }
               
            }
            catch (Exception)
            {

                // throw;
            }

            return Content("0");
        }
    }
}
